//
//  FSUserLoginViewController.m
//  fanshu
//
//  Created by jklin on 16/3/28.
//  Copyright © 2016年 FS. All rights reserved.
//

#import "FSUserLoginViewController.h"
#import "FSMineCoverView.h"
#import "FSPWDPromptView.h"
#import "FSWebViewController.h"
#import "FSNewIDController.h"
#import "FSMineViewController.h"

#import "AFNetworking.h"
#import "SVProgressHUD.h"
#import "MJExtension.h"
#import "FSUser.h"

@interface FSUserLoginViewController ()<UITextFieldDelegate>
@property (nonatomic, weak) FSPWDPromptView *pwdPromptView;
@property (nonatomic, weak) FSMineCoverView *coverView;

@property (weak, nonatomic) IBOutlet UIButton *PWDPrompt;
@property (weak, nonatomic) IBOutlet UITextField *accountText;
@property (weak, nonatomic) IBOutlet UITextField *PWDText;

@end

@implementation FSUserLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //修改为圆形
    self.navigationController.navigationBarHidden = YES;
    self.PWDPrompt.layer.cornerRadius = self.PWDPrompt.frame.size.height * 0.5;
    self.PWDPrompt.layer.masksToBounds = YES;
    //添加点击方法
    [self.PWDPrompt addTarget:self action:@selector(showPrompt) forControlEvents:UIControlEventTouchDown];
    self.PWDText.secureTextEntry = YES;
    
}

-(void)showPrompt{
    FSPWDPromptView *PWDPromptVC = [[FSPWDPromptView alloc]init];
    self.pwdPromptView = PWDPromptVC;
    [self.pwdPromptView.resetPWDBtn addTarget:self action:@selector(resetPWDBtnClick) forControlEvents:UIControlEventTouchDown];
    [self.pwdPromptView.phoneCheckBtn addTarget:self action:@selector(phoneCheckBtnClick) forControlEvents:UIControlEventTouchDown];
    
    CGPoint point = self.view.center;
    
    FSMineCoverView *coverView = [FSMineCoverView showCoverView];
    [PWDPromptVC showPWDPromptViewAtPoint:point];
    
    self.coverView = coverView;
    //结束编辑状态
    [self.PWDText resignFirstResponder];
    [self.accountText resignFirstResponder];
    

   
}
- (IBAction)cancleBtn:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//提示信息跳转
-(void)resetPWDBtnClick{

    

    FSWebViewController *vc = [[FSWebViewController alloc]init];

    [self presentViewController:vc animated:YES completion:nil];
    
    [self.coverView removeCoverView];
}
//判断登录
- (IBAction)loginBtn:(id)sender {
    //开启HUD
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    //1.先拿到用户的输入
    NSString *usernameStr = self.accountText.text;
    NSString *pwdStr = self.PWDText.text;
    
    //检查用户输入是否为空
    if (usernameStr.length == 0) {
        [SVProgressHUD showErrorWithStatus:self.accountText.placeholder];
        return;
    }
    
    if (pwdStr.length == 0) {
        [SVProgressHUD showErrorWithStatus:self.PWDText.placeholder];
        return;
    }
    
    //发送请求,加载数据
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    NSDictionary *dictM = @{
                                   @"username":usernameStr,
                                   @"pwd":pwdStr,
                                   @"type":@"JSON"
                                   };
[sessionManager POST:@"" parameters:dictM constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

    NSDictionary *userDict = [responseObject[@"user"] firstObject];
    FSUser *user = [FSUser mj_objectWithKeyValues:userDict];
    //将数据存储
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *fileName = [filePath stringByAppendingPathComponent:@"userData.plist"];
   [NSKeyedArchiver archiveRootObject:user toFile:fileName];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"login"];
    
    //跳转回  我的  界面
    FSMineViewController *mineVC = [[FSMineViewController alloc]init];
    [self.navigationController pushViewController:mineVC animated:YES];
    
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    [SVProgressHUD showErrorWithStatus:@"账号/密码错误"];
}];

}

//跳转注册界面
- (IBAction)newIDBtn:(id)sender {
    FSNewIDController *NewID = [[FSNewIDController alloc]init];
    [self.navigationController pushViewController:NewID animated:YES];
}


-(void)phoneCheckBtnClick{


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.accountText resignFirstResponder];
    [self.PWDText resignFirstResponder];
    
}


@end
